package young.Util;


import young.frame.Atm;
import young.pojo.UserDO;

import java.sql.*;

public class DBUtil {
    private Atm frame;
    Sql sql=new Sql();


    //调取数据
    public UserDO getByUsernameAndPwd(String username, String pwd) {
        UserDO userDO = null;
        //创建连接
        Connection con = sql.getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        String sql = "select * from t_atm where atm_id=? and atm_pwd=?";
        //查询调取
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, username);
            statement.setObject(2, pwd);
            rs = statement.executeQuery();


            while (rs.next()) {
                userDO = new UserDO();
                userDO.setId(rs.getString(1));
                userDO.setPassword(rs.getString(2));
                userDO.setMoney(rs.getInt(3));


            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                if (statement != null) {
                    try {
                        statement.close();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                    if (rs != null) {
                        try {
                            rs.close();
                        } catch (SQLException throwables) {
                            throwables.printStackTrace();
                        }
                    }
                }
            }

        }
        return userDO;


    }


    //修改保存数据
//    public void insert(Atm frame) {
//        this.frame = frame;
//        Connection con = sql.getConnection();
//        Statement statement = null;
//        try {
//            statement = con.createStatement();
//
//            int money = frame.getLoginUser().getMoney();
//            String id = frame.getLoginUser().getId();
//            String pwd = frame.getLoginUser().getPassword();
//
//            String mysql = "update t_atm set atm_money='" + money + "' where atm_id='" + id + "'";
//            String mysql1 = "update t_atm set atm_pwd='" + pwd + "' where atm_id='" + id + "'";
//
//            //修改执行
//            statement.executeUpdate(mysql);
//            statement.executeUpdate(mysql1);
//
//
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        } finally {
//            guanbi(con, statement);
//        }




    public static void guanbi(Connection con, Statement statement) {
        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public UserDO getById(String id) {
        Connection con = sql.getConnection();
        PreparedStatement Statement = null;
        ResultSet rs = null;
        UserDO userDO = null;
        String sql = "select * from t_atm  where atm_id=?";
        try {
            Statement = con.prepareStatement(sql);
            Statement.setObject(1, id);
            rs = Statement.executeQuery();

            if (rs.next()) {
                userDO = new UserDO();
                userDO.setId(rs.getString(1));
                userDO.setPassword(rs.getString(2));
                userDO.setMoney(rs.getInt(3));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            guanbi(con, Statement, rs);
        }
        return userDO;
    }


    /**
     * 根据User对象中的数据进行修改
     *
     * @param
     */
    public void update(UserDO userDO1, UserDO userDO2) {
        // 1. 创建连接
        Connection con = sql.getConnection();
        PreparedStatement statement = null;
        String sql = "update t_atm set atm_pwd=?,atm_money=? where atm_id=?";
        try {
            // 2. 获取执行SQL语句对象
            con.setAutoCommit(false);
            statement = con.prepareStatement(sql);
            statement.setObject(1, userDO1.getPassword());
            statement.setObject(2, userDO1.getMoney());
            statement.setObject(3, userDO1.getId());
            statement.executeUpdate();

            statement.setObject(1, userDO2.getPassword());
            statement.setObject(2, userDO2.getMoney());
            statement.setObject(3, userDO2.getId());
            statement.executeUpdate();
            con.commit();

        } catch (SQLException throwables) {
            throwables.printStackTrace();

            try {
                con.rollback(); //异常回滚
            } catch (SQLException e) {
                e.printStackTrace();
            }


        } finally {
            // 4. 关闭连接
            guanbi(con, statement);
        }
    }


    private void guanbi(Connection con, Statement statement, ResultSet rs) {

    }


}